﻿using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using Baimasoft.FrameWork.Base;
using Baimasoft.Manage.SqlServerDAL;

public partial class SysManage_OrgPersTree_Pers_Edit : System.Web.UI.Page
{
    CommClass MyCl = new CommClass();
    DeptPersManage MyDPM = new DeptPersManage();
    PostManage MyPM = new PostManage();

    public string sOrgCode = "", sOrgType = "0";

    protected void Page_Load(object sender, EventArgs e)
    {
        if (ViewState["VOrgType"] == null || ViewState["VOrgType"].ToString().CompareTo("") == 0)
        {  //已被使用
            ViewState["VOrgType"] = "0";
        }
        try
        {
            ViewState["VOrgType"] = Request.QueryString["OrgType"].ToString();
            HOrgType.Value = ViewState["VOrgType"].ToString().Trim();
        }
        catch
        {
        }
        if (!Page.IsPostBack)
        {
            DutyBind(ViewState["VOrgType"].ToString().Trim());
        }
    }

    //默认数据绑定
    public void DutyBind(string sOrgType)
    {
        DutyDL.DataSource = MyPM .BackView (10,"2");
        DutyDL.DataTextField = "post_name";
        DutyDL.DataValueField = "Post_ID";
        DutyDL.DataBind();
        DutyDL.Items.Insert(0, "选择职务类别");
        DutyDL.Items[0].Value = "0";
    }

    protected void GrantBtn1_ServerClick(object sender, EventArgs e)
    {
        string sPersCode = MyCl .CutLimitString (PersCode.Text.ToString().Trim());  //员工号

        string shPersCode = MyCl.CutLimitString(HPersCode.Value.ToString().Trim());
        string sPersID = hPersID.Value.ToString().Trim();    //流水号

        string sEditType = hidEditType.Value.ToString().Trim();  //类别 1新建 2 修改
        string sName = MyCl.CutLimitString(Name.Text.ToString().Trim());              //人员名称 
        string sSex = Sex.SelectedValue.ToString().Trim();     //性别
        string sBirth = MyCl.CutLimitString(Birth.Text.ToString().Trim());          //出生年月
        string sCardID = MyCl.CutLimitString(CardID.Text.ToString().Trim());        //身份证

        //string OrID = hOrgID1.Value.ToString().Trim();        //组织ID
        //string[] sOrgID = OrID.Split(',');
        string sOldOrgID = HChoiceID_2_1.Value.ToString().Trim();
        string sNewOrgID = HChoiceID_2.Value.ToString().Trim();

        //string NewOID = hNewOrgID1.Value.ToString().Trim();       //新选择的组织ID
        //string[] sNewOrgID = NewOID.Split(',');

        string OrgPrincinal = OrgPricinal.SelectedValue.ToString().Trim();//是否该部门负责人
        string sDuty = DutyDL.SelectedValue.ToString().Trim();  //职务名称
        string sPhone = MyCl.CutLimitString(Phone.Text.ToString().Trim());             //固定电话
        string sMobile = MyCl.CutLimitString(Mobile.Text.ToString().Trim());           //手机/小灵通 
        string sPOrder = MyCl.CutLimitString(POrder.Text.ToString().Trim());           //排序号 
        string sEffective = Effective.SelectedValue.ToString().Trim();  //是否有效
        string sUserRole = Session["Baimasoft_Role_Code"].ToString().Trim();       //当前用户角色 
        string sOrgPersID = HOrgPersID.Value.ToString().Trim();              //组织人员ID

        if (sPersCode == "" || sPersCode == null)
        {
            ClientScript.RegisterStartupScript(this.GetType(), "", "<script>alert('员工号不能为空!');</script>");
            return;
        }
        if (sName == "" || sName == null)
        {
            ClientScript.RegisterStartupScript(this.GetType(), "", "<script>alert('人员名称不能为空!');</script>");
            return;
        }

        if (sEditType == "2")  //修改
        {
            if (shPersCode.Trim() != sPersCode.Trim())
            {
                int ibackpd = MyDPM .GetPersCodeNum (sPersCode);
                if (ibackpd > 0)
                {
                    ClientScript.RegisterStartupScript(this.GetType(), "", "<script>alert('员工号" + sPersCode + "已存在!');</script>");
                }
            }

            string sIfSavehistoryInfo = "0";
            if (IfsavePersHistoryInfo.Checked == true)
            {
                sIfSavehistoryInfo = "1";
            }

            string sBackPersName = MyDPM .JudgePersIDInOrgID (sNewOrgID.Trim(), sPersID.Trim());
            if (sBackPersName.Trim() != "" && sOldOrgID.Trim() != sNewOrgID.Trim())
            {
                ClientScript.RegisterStartupScript(this.GetType(), "", "<script>alert('[" + ChoiceName_2 .Text.ToString ().Trim () + "]下已存在人员[" + Name.Text.ToString().Trim() + "]');</script>");
                return;
            }
            else
            {
                int iBack = 0;
                iBack = MyDPM .UpOrgPersInfo (sOrgPersID, sPersCode, sPersID, sName, sSex, sBirth, sCardID, sNewOrgID, sOldOrgID, sDuty, sPhone, sMobile, sPOrder, sUserRole, sEffective, OrgPrincinal, sIfSavehistoryInfo, ChangeReason.Text.ToString().Trim());
                if (iBack == 1)
                {
                    //UMConn.UpOtherChildNumber (sOrgID, sNewOrgID);  //当所属组织ID没有变动时
                    ClientScript.RegisterStartupScript(this.GetType(), "", "<script>alert('修改成功!');window.close();</script>");   //location.href='OrgTree_List.aspx';
                    return;
                }
                else
                {
                    ClientScript.RegisterStartupScript(this.GetType(), "", "<script>alert('修改失败!');</script>");
                    return;
                }
            }
        }
        else if (sEditType == "1")
        {
            int ibackPD = MyDPM.GetPersCodeNum(sPersCode);
            if (ibackPD > 0)
            {
                ClientScript.RegisterStartupScript(this.GetType(), "", "<script>alert('员工号" + sPersCode + "已存在!');</script>");
            }
            else
            {
                int iback2 = 0;
                iback2 = MyDPM .SaveOrgPersInfo (sPersCode, sName, sSex, sBirth, sCardID, sNewOrgID, sOldOrgID, sDuty, sPhone, sMobile, sPOrder, sUserRole, sEffective, OrgPrincinal);
                if (iback2 == 1)
                {
                    //UMConn.UpOrgPersNumber (sOrgID, sNewOrgID);  //当所属组织ID没有变动时
                    ClientScript.RegisterStartupScript(this.GetType(), "", "<script>alert('新建成功!');window.close();</script>");
                    return;
                }
                else
                {
                    ClientScript.RegisterStartupScript(this.GetType(), "", "<script>alert('新建失败!');</script>");
                    return;
                }
            }
        }
    }
}
